using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Drawing;
using System.Data.Odbc;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.Windows.Media;

using Newtonsoft.Json.Linq;

using Styx;
using Styx.Common;
using Styx.WoWInternals;
/*
 * //Zone Changeing Events Here
 * Lua.Events.AttachEvent("ZONE_CHANGED", LuaEvents.ZONE_CHANGED);
 * Lua.Events.AttachEvent("ZONE_CHANGED_NEW_AREA", LuaEvents.ZONE_CHANGED_NEW_AREA);
 * 
 * 
 * 
 */
namespace WatchMyToon
{
    public class Lua_Events
    {
        public Fucntions_Unit UnitFunctions = new Fucntions_Unit();
        public SQL_Connector SQLConnector = new SQL_Connector();
        
        
        // LOOT Event - Needs Work (Money Loot, Cleanup ?) 08/08/2012 - CoDeRs
        public void Loot(object sender, LuaEventArgs args)
        {            
            try
            {
                WatchMyToon.bIsLootClosed = false;

                string looted = args.Args[0].ToString();
                Match match = Regex.Match(looted, @"You receive loot:.*\|\S{3}(.*)\|Hitem:(\d{1,10}):.*\[(.{1,40})\]\|h\|(?=rx(\d{1,3})|r)");
                if (match.Success)
                {
                    string Amount;

                    if (match.Groups[4].Value == "r" || match.Groups[4].Value == "")
                        Amount = "1";
                    else
                        Amount = match.Groups[4].Value;

                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABLoots\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szColor\":\"" + match.Groups[1].Value + "\"},{\"szId\":\"" + match.Groups[2].Value + "\"},{\"szName\":\"" + match.Groups[3].Value + "\"},{\"szAmount\":\"" + Amount + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Loot Update Report: " + sCommandCheck);
                    
                }
                
            }
            catch (ArgumentException ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - LOOT Event SQL Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // LootClosed Event
        public void LootClosed(object sender, LuaEventArgs args)
        {
            try
            {
                WatchMyToon.bIsLootClosed = true;
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - LootClosed LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // BagUpdate Event
        public void BagUpdate(object sender, LuaEventArgs args)
        {
            try
            {
                if (WatchMyToon.bIsLootClosed == true)
                {
                    try
                    {
                        string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"nFreeBagSlot\":\"" + StyxWoW.Me.FreeBagSlots + "\"},{\"nTotalBagSlot\":\"" + UnitFunctions.getTotalBagCount() + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                        Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Bag Update Report: " + sCommandCheck);
                    }
                    catch (ArgumentException ex)
                    {
                        Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - BagUpdate SQL Failed!");
                        Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - BagUpdate LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // PLAYER_MONEY Event
        public void PLAYER_MONEY(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szMoneyInCopper\":\"" + StyxWoW.Me.Copper.ToString() + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Gold Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_MONEY SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_MONEY LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // ACHIEVEMENT Event - Complete 08/08/2012 - CoDeRs (IMO)
        public void ACHIEVEMENT_EARNED(object sender, LuaEventArgs args)
        {
            try
            {
                List<string> achievementInfo = Lua.GetReturnValues("return GetAchievementInfo(" + args.Args[0].ToString() + ")");

                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABAchievements\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"nAchievLfd\":\"NULL\"},{\"szAchievId\":\"" + achievementInfo[0] + "\"},{\"szAchievName\":\"" + achievementInfo[1] + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Achievement Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - ACHIEVEMENT_EARNED SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - ACHIEVEMENT_EARNED LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // QUEST_ACCEPTED Event - Complete 08/08/2012 - CoDeRs (IMO)
        public void QUEST_ACCEPTED(object sender, LuaEventArgs args)
        {
            try
            {
                string questTitle = string.Empty;
                string questlevel = string.Empty;
                string questID = string.Empty;
                string isComplete = string.Empty;

                List<string> QuestLua = Lua.GetReturnValues("return GetQuestLogTitle(" + args.Args[0].ToString() + ")");

                questTitle = QuestLua[0];
                questlevel = QuestLua[1];
                isComplete = QuestLua[6];
                questID = QuestLua[8];

                if (isComplete == "nil")
                {
                    isComplete = "0";
                }

                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABQuestLog\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"nQuestId\":\"" + Convert.ToInt32(questID) + "\"},{\"szQuestName\":\"" + questTitle + "\"},{\"nQuestLevel\":\"" + Convert.ToInt32(questlevel) + "\"},{\"nComplete\":\"" + Convert.ToInt32(isComplete) + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Quest Accepted Insert Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - QUEST_WATCH_ACCEPTED SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - QUEST_WATCH_ACCEPTED LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // QUEST_UPDATE Event - Complete 08/08/2012 - CoDeRs (IMO) - Bugged.... Also needs a second AND Condition
        public void QUEST_WATCH_UPDATE(object sender, LuaEventArgs args)
        {
            try
            {
                string questTitle = string.Empty;
                string questlevel = string.Empty;
                string questID = string.Empty;
                string isComplete = string.Empty;

                List<string> QuestLua = Lua.GetReturnValues("return GetQuestLogTitle(" + args.Args[0].ToString() + ")");

                questTitle = QuestLua[0];
                questlevel = QuestLua[1];
                isComplete = QuestLua[6];
                questID = QuestLua[8];

                Logging.Write(LogLevel.Normal, Colors.Yellow, "QuestID: " + questID + " - " + questTitle + " - Complete: " + isComplete);

                if (isComplete == "1")
                {
                    try
                    {
                        string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABQuestLog\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"nComplete\":\"" + Convert.ToInt32(isComplete) + "\"}]", "{\"Query\":\"UpdateMulti\"}", "{\"nQuestId\":\"" + Convert.ToInt32(questID) + "\"}");
                        Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Quest Update Report: " + sCommandCheck);
                    }
                    catch (Exception ex)
                    {
                        Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - QUEST_WATCH_UPDATE SQL Failed!");
                        Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - QUEST_WATCH_UPDATE LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // PLAYER_ENTERING_WORLD Event
        public void PLAYER_ENTERING_WORLD(object sender, LuaEventArgs args)
        {
            try
            {
                WatchMyToon.bIsInLoadingScreen = true;
                Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - Player Waiting In Loading Screen.. Please Wait.");
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_ENTERING_WORLD LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // PLAYER_LEVEL_UP Event
        public void PLAYER_LEVEL_UP(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"nCurrentLvl\":\"" + StyxWoW.Me.Level + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Level Up Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_LEVEL_UP SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_LEVEL_UP LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // PLAYER_GUILD_UPDATE Event
        public void PLAYER_GUILD_UPDATE(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    if (!UnitFunctions.isInGuild())
                    {
                        string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"bIsInGuild\":\"" + UnitFunctions.isInGuild() + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                        Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Guild ??What?? Update Report: " + sCommandCheck);
                    }
                    else
                    {
                        Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - PLAYER_GUILD_UPDATE Fired for Some Reason??");
                    }
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_GUILD_UPDATE SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_GUILD_UPDATE LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // PLAYER_XP_UPDATE Event
        public void PLAYER_XP_UPDATE(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"nCurrentXp\":\"" + StyxWoW.Me.Experience + "\"},{\"nNextLvlXp\":\"" + StyxWoW.Me.NextLevelExperience + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Experence Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_XP_UPDATE SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_XP_UPDATE LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // UNIT_POWER Event
        public void UNIT_POWER(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    if (args.Args[0].ToString() == "player")
                    {
                        string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szPowerType\":\"" + StyxWoW.Me.PowerType.ToString() + "\"},{\"szPower\":\"" + StyxWoW.Me.PowerPercent.ToString() + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                        Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Power Update Report: " + sCommandCheck);
                    }
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - UNIT_POWER SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - UNIT_POWER LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // UNIT_HEALTH Event
        public void UNIT_HEALTH(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    if (args.Args[0].ToString() == "player")
                    {
                        string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szHealth\":\"" + StyxWoW.Me.HealthPercent.ToString() + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                        Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Health Update Report: " + sCommandCheck);
                    }
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - UNIT_HEALTH SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - UNIT_HEALTH LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // ZONE_CHANGED Event
        public void ZONE_CHANGED(object sender, LuaEventArgs args)
        {
            try
            {
                UnitFunctions.updateCurrentPosition(); 
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - ZONE_CHANGED LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // ZONE_CHANGED_INDOORS Event
        public void ZONE_CHANGED_INDOORS(object sender, LuaEventArgs args)
        {
            try
            {
                UnitFunctions.updateCurrentPosition();
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - ZONE_CHANGED_INDOORS LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // ZONE_CHANGED_NEW_AREA Event
        public void ZONE_CHANGED_NEW_AREA(object sender, LuaEventArgs args)
        {
            try
            {                
                if (WatchMyToon.bIsInLoadingScreen)
                {
                    WatchMyToon.bIsInLoadingScreen = false;
                    Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - Player Entered Zone From Loading Screen!");
                }
                UnitFunctions.updateCurrentPosition();
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - ZONE_CHANGED_NEW_AREA LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // GUILD_INVITE_REQUEST Event - Complete 08/08/2012 - CoDeRs (IMO)
        public void GUILD_INVITE_REQUEST(object sender, LuaEventArgs args)
        {
            try
            {
                string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABGuildInvites\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"}]", "{\"Query\":\"Clear\"}", "{\"Limit\":\"0\"}");
                Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Guild Invites Clear Report: " + sCommandCheck);

                sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABGuildInvites\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szInviterName\":\"" + args.Args[0].ToString() + "\"},{\"szGuildName\":\"" + args.Args[1].ToString() + "\"},{\"szStatus\":\"REQUEST_GROUP\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Guild Invite Insert Report: " + sCommandCheck);
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - GUILD_INVITE_REQUEST SQL Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // GUILD_INVITE_CANCEL Event - Complete 08/08/2012 - CoDeRs (IMO)
        public void GUILD_INVITE_CANCEL(object sender, LuaEventArgs args)
        {

            try
            {
                //Left Guild??
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - GUILD_INVITE_CANCLE SQL Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }
       
        // LUA_EVENT_TEMPLATE Event
        public void GUILD_XP_UPDATE(object sender, LuaEventArgs args)
        {
            try
            {
                List<string> currentGuildInfo = new List<string>();
                List<string> currentGuildLevel = new List<string>();
                List<string> currentGuildExperence = new List<string>();
                List<string> currentGuildRosterContrib = new List<string>();

                try
                {
                    currentGuildInfo = Lua.GetReturnValues("return GetGuildInfo(UnitName(\"Player\"))");
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - GetGuildInfo LUA Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }

                try
                {
                    currentGuildLevel = Lua.GetReturnValues("return GetGuildLevel()");
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - GetGuildLevel LUA Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
                try
                {
                    currentGuildExperence = Lua.GetReturnValues("return UnitGetGuildXP(\"Player\")");
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - UnitGetGuildXP LUA Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }

                try
                {
                    List<string> Offline = Lua.GetReturnValues("return SetGuildRosterShowOffline(false)");
                    List<string> TotalOnlineMembers = Lua.GetReturnValues("return GetNumGuildMembers()");

                    int iTotalOnlineMemeber = Convert.ToInt32(TotalOnlineMembers[1].ToString());
                    
                    if( iTotalOnlineMemeber > 0 )
                    {
                        for (int i = 1; i <= iTotalOnlineMemeber; i++)
                        {
                            List<string> CurrentOnlineMemeber = Lua.GetReturnValues("return GetGuildRosterInfo(" + i + ")");
                            if (CurrentOnlineMemeber[0].ToString() == StyxWoW.Me.Name)
                            {
                                currentGuildRosterContrib = Lua.GetReturnValues("return GetGuildRosterContribution(" + i + ")");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - GuildRoster LUA Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex);
                }

                try
                {
                    string sql = "[{\"TAB\":\"TABGuildInformation\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szName\":\"" + currentGuildInfo[0] + "\"},{\"szRank\":\"" + currentGuildInfo[1] + "\"},{\"nLevel\":\"" + currentGuildLevel[0] + "\"},{\"nCurrentXP\":\"" + currentGuildExperence[0] + "\"},{\"nNextLevelXP\":\"" + currentGuildExperence[1] + "\"},{\"nWeeklyContribution\":\"" + currentGuildRosterContrib[0] + "\"},{\"nTotalContribution\":\"" + currentGuildRosterContrib[1] + "\"}]";
                    string sCommandCheck = SQLConnector.SendJSON(sql, "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Guild Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - GUILD_XP_UPDATE SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - GUILD_XP_UPDATE LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // PARTY_INVITE_REQUEST Event - Complete 08/08/2012 - CoDeRs (IMO)
        public void PARTY_INVITE_REQUEST(object sender, LuaEventArgs args)
        {
            try
            {
                string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABGroupInvites\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"}]", "{\"Query\":\"Clear\"}", "{\"Limit\":\"0\"}");
                Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Party Invites Clear Report: " + sCommandCheck);

                sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABGroupInvites\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szInviterName\":\"" + args.Args[0].ToString() + "\"},{\"szStatus\":\"REQUEST_GROUP\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Party Invite Insert Report: " + sCommandCheck);
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PARTY_INVITE_REQUEST SQL Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }

        }

        // PARTY_INVITE_CANCEL Event - Complete 08/08/2012 - CoDeRs (IMO)
        public void PARTY_INVITE_CANCEL(object sender, LuaEventArgs args)
        {
            try
            {
                //Left Group...
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PARTY_INVITE_CANCEL SQL Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // RAID_TARGET_UPDATE Event
        public void RAID_TARGET_UPDATE(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"bIsInParty\":\"" + Convert.ToInt32(StyxWoW.Me.IsInParty.ToString()).ToString() + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Raid/Group Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - RAID_TARGET_UPDATE SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - RAID_TARGET_UPDATE LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // UPDATE_FACTION Event - Complete 08/08/2012 - CoDeRs (Would like to make this less "work")
        public void UPDATE_FACTION(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    UnitFunctions.updateReputations();
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - UPDATE_FACTION SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - UPDATE_FACTION LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // GMRESPONSE_RECEIVED Event - Creates a Row but no details... No Way to test :(  08/08/2012 - CoDeRs
        public void GMRESPONSE_RECEIVED(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABMessageReceive\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szMessageType\":\"GM_MESSAGE\"},{\"szMessagePartner\":\"GameMaster\"},{\"szMessageText\":\"Unknown\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Game Master Responce Insert Report: " + sCommandCheck);
                    
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - GMRESPONSE_RECEIVED SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - GMRESPONSE_RECEIVED LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // SCREENSHOT_SUCCEEDED Event - Needs Work (Testing) 08/08/2012 - CoDeRs
        public void SCREENSHOT_SUCCEEDED(object sender, LuaEventArgs args)
        {
			try
			{
				string filename = Application.StartupPath + "\\Plugins\\WatchMyToon\\ScreenShots\\" + StyxWoW.Me.Name + "_" + WatchMyToon.nToonId + ".png";
                Bitmap bmpScreenShot = new Bitmap(@filename);

				try
				{
					//WoWScrnShot_MMDDYY_HHMMSS	
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABScreenShots\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"picScreenShot\":\"" + bmpScreenShot + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Screenshot Update Report: " + sCommandCheck);
				}
				catch (Exception ex)
				{
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - SCREENSHOT_SUCCEEDED SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
				}
			}
			catch (Exception ex)
			{
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - SCREENSHOT_SUCCEEDED LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
			}
        }

        // SCREENSHOT_FAILED Event - Complete 08/08/2012 - CoDeRs (IMO)
        public void SCREENSHOT_FAILED(object sender, LuaEventArgs args)
        {
            try
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - Screen Capture Failed!");
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - SCREENSHOT_FAILED LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // HONOR_CURRENCY_UPDATE Event - Needs Changed New Currency System
        public void HONOR_CURRENCY_UPDATE(object sender, LuaEventArgs args)
        {
            try
            {
                List<string> HonorPoints = Lua.GetReturnValues("return GetHonorCurrency()");
                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"nHonorPoints\":\"" + HonorPoints[0] + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Honor Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - HONOR_CURRENCY_UPDATE SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - HONOR_CURRENCY_UPDATE LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // LFG_COMPLETION_REWARD Event - Needs Changed New Currency System
        public void LFG_COMPLETION_REWARD(object sender, LuaEventArgs args)
        {
            try
            {
                List<string> JusticePoints = Lua.GetReturnValues("return GetCurrencyInfo(395)");
                List<string> ValorPoints = Lua.GetReturnValues("return GetCurrencyInfo(396)");
                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"nJusticePoints\":\"" + JusticePoints[0] + "\"},{\"nValorPoints\":\"" + ValorPoints[0] + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - LFG Completion Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - LFG_COMPLETION_REWARD SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - LFG_COMPLETION_REWARD LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // ACTIVE_TALENT_GROUP_CHANGED Event - Complete 08/08/2012 - CoDeRs (IMO)
        public void ACTIVE_TALENT_GROUP_CHANGED(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToons\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szCurrentSpec\":\"" + UnitFunctions.getSpec() + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Talent Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - ACTIVE_TALENT_GROUP_CHANGED SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - ACTIVE_TALENT_GROUP_CHANGED LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // PLAYER_DEAD Event - Complete 08/08/2012 - CoDeRs (IMO)
        public void PLAYER_DEAD(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"bIsDead\":\"True\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Player Dead Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_DEAD SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
                 
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_DEAD LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // PLAYER_UNGHOST Event - Complete 08/08/2012 - CoDeRs (IMO)
        public void PLAYER_UNGHOST(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"bIsDead\":\"False\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Player Alive Update Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_UNGHOST SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - PLAYER_UNGHOST LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }


        // LUA_EVENT_TEMPLATE Event
        public void LUA_EVENT_TEMPLATE(object sender, LuaEventArgs args)
        {
            try
            {
                try
                {
                    SQLConnector.SendJSON("[{\"TAB\":\"TABGroupInvites\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szStatus\":\"DECLINE_GROUP\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - LUA_EVENT_TEMPLATE SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - LUA_EVENT_TEMPLATE LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

    }
}
